#!/usr/bin/env python

import os

symbolTab={}
tabs = -1
tabC = 3
def printEmptyTabs():
	global tabs
	for i in range((tabs)*4):
		if i%4==0:	
			printchar = '|'
		else:
			printchar = ' '
		print printchar,
	print
			
def printTabs():
	global tabs
	str=""
	count=0
	if tabs==0:
		return
	
	for i in range(tabs-1):
		for j in range(4):
			if count%4 == 0:
				print '|', 
			else:
				print ' ',
			count+=1
	count =0
	for j in range(4):
		if count == 0:
			print '+', 
		else:
			print '-',
		count+=1
def loadNMFile(filename):
	for line in os.popen('nm -g --defined-only '+filename).readlines():
		try:
			a,t,n=line.split()
			if t == 'T':
				symbolTab[a.lstrip('0')]=n
		except:
			continue
def printTrace(filename):
	global tabs
	f=open(filename)
	for line in f.readlines():
		try:
			sym,addr = line.split()
			if sym == "++": 
				tabs+=1
				# Dirty hack to give some Tree'ish look and space b/w lines
				printEmptyTabs()
			printTabs()
			print symbolTab[addr.rstrip('\n')]
			if sym == '--':	
				printEmptyTabs()
				tabs-=1
		except:
			continue

def main():
	loadNMFile('./ltrace')
	printTrace("test.out")

if __name__== '__main__':
	main()









#print [{line.split()[0]:line.split()[2]} for line in open("nm.out").readlines() if line.split()[1]=='T']
